<?php
namespace app\admin\controller;
use app\common\validate\BlockValidate;
use app\admin\model\Block as BlockModel;
use think\Db;
use think\facade\Cache;
class Block extends Common {
	
	public function index(){
		return $this->fetch();
	}
	public function index_lists(){
		$limit = request()->param('limit');
		$page = request()->param('page');
		$start = ($page-1)*$limit;
		$count = db('block')->count();
		$result = db('block')
				  ->limit($start,$limit)
				  ->order('id desc,sort desc')
				  ->cache('block'.$limit.$page,3600,'block')
				  ->select();
		
		return showTable(0, 'OK', $count ,$result, 200);
		
	}
	//添加
	public function add(){
		if(request()->isPost()){
			$data = request()->param();
			$data['create_time'] = date('Y-m-d h:i:s');		
			$validate = new BlockValidate;
			if (!$validate->scene('add')->check($data)) {
				return show(0, $validate->getError(), 0 , 200);
			}else{
				Cache::clear('block');
				$rest = (new BlockModel())->strict(false)->insert($data);
				return show(1, '添加成功', $rest, 200);
			}			
		}
		return $this->fetch();
	}
}